#!/bin/bash

echo "=== 测试跟进API增删改查全流程 ==="

# 设置token
TOKEN="Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJvYS5nb3VndWNtcy5jb20iLCJhdWQiOiJnb3VndW9hIiwiaWF0IjoxNzY0MDMzNDA1LCJleHAiOjE3NjQxMTk4MDUsInN1YiI6MSwiZGF0YSI6eyJ1c2VyaWQiOjF9fQ.LLgWZm7YDB6AtxDLMLxLVvDH_j_ea7n_8iAMcWgmI-M"

# 设置请求头
HEADERS="-H 'Content-Type: application/json' -H 'Authorization: $TOKEN'"

echo ""
echo "================================"
echo "1. 测试获取跟进选项接口..."
echo "================================"
curl -s $HEADERS "http://127.0.0.1:8000/api/crm/followup/getFollowupOptions" | python3 -m json.tool

echo ""
echo "================================"
echo "2. 测试获取跟进类型选项..."
echo "================================"
curl -s $HEADERS "http://127.0.0.1:8000/api/crm/followup/typeOptions" | python3 -m json.tool

echo ""
echo "================================"
echo "3. 测试创建常规跟进（opportunity_id=0）..."
echo "================================"
CREATE_RESPONSE=$(curl -s $HEADERS -X POST "http://127.0.0.1:8000/api/crm/followup/create" \
  -d '{
    "customer_id": 1,
    "opportunity_id": 0,
    "followup_type": 1,
    "followup_time": "2025-01-15 14:30:00",
    "content": "这是测试的常规跟进记录"
  }')

echo "创建跟进响应:"
echo $CREATE_RESPONSE | python3 -m json.tool

# 提取跟进ID
FOLLOWUP_ID=$(echo $CREATE_RESPONSE | grep -o '"id":[0-9]*' | cut -d':' -f2)
echo "跟进ID: $FOLLOWUP_ID"

echo ""
echo "================================"
echo "4. 测试创建机会跟进（opportunity_id>0）..."
echo "================================"
CREATE_OPP_RESPONSE=$(curl -s $HEADERS -X POST "http://127.0.0.1:8000/api/crm/followup/create" \
  -d '{
    "customer_id": 1,
    "opportunity_id": 1,
    "followup_type": 2,
    "followup_time": "2025-01-16 10:00:00",
    "content": "这是测试的机会跟进记录"
  }')

echo "创建机会跟进响应:"
echo $CREATE_OPP_RESPONSE | python3 -m json.tool

echo ""
echo "================================"
echo "5. 测试获取跟进列表..."
echo "================================"
curl -s $HEADERS "http://127.0.0.1:8000/api/crm/followup/list?page=1&limit=10" | python3 -m json.tool

if [ -n "$FOLLOWUP_ID" ]; then
  echo ""
  echo "================================"
  echo "6. 测试获取跟进详情..."
  echo "================================"
  curl -s $HEADERS "http://127.0.0.1:8000/api/crm/followup/detail?id=$FOLLOWUP_ID" | python3 -m json.tool

  echo ""
  echo "================================"
  echo "7. 测试编辑跟进..."
  echo "================================"
  EDIT_RESPONSE=$(curl -s $HEADERS -X POST "http://127.0.0.1:8000/api/crm/followup/edit" \
    -d "{
      \"id\": $FOLLOWUP_ID,
      \"content\": \"这是编辑后的跟进内容\",
      \"followup_type\": 3
    }")

  echo "编辑跟进响应:"
  echo $EDIT_RESPONSE | python3 -m json.tool

  echo ""
  echo "================================"
  echo "8. 测试删除跟进..."
  echo "================================"
  DELETE_RESPONSE=$(curl -s $HEADERS -X POST "http://127.0.0.1:8000/api/crm/followup/del" \
    -d "{\"id\": $FOLLOWUP_ID}")

  echo "删除跟进响应:"
  echo $DELETE_RESPONSE | python3 -m json.tool
fi

echo ""
echo "================================"
echo "9. 再次获取跟进列表（验证删除）..."
echo "================================"
curl -s $HEADERS "http://127.0.0.1:8000/api/crm/followup/list?page=1&limit=10" | python3 -m json.tool

echo ""
echo "=== 测试完成 ==="
